package com.tedu.entity;

import java.awt.*;
import java.net.URL;
import java.util.Random;

/**
 * 坦克实体类
 * e-r图实现
 */
public class TankEntity {
    //属性
    private int x,y,w,h;

    //图片
    private Image bg;
    //方向  直接使用dir枚举类型
    private Dir dir;

    //alt+ insert  setget方法  tostring方法  构造方法
    //画
    public void draw(Graphics g){
        g.drawImage(bg,x,y,w,h,null);
    }
    //坦克移动
    public void move(){
        switch (dir){
            case up:// 每次移动5个像素点
                y = y -1;
                break;  // 退出循环
            case down:
                y = y + 1;
                break;
            case left:
                x = x - 1;
                break;
            case right:
                x = x + 1;
                break;
        }
        //坦克能不能飞出游戏窗口
        if(y<0||x<0||x>880-72||y>660-72){
            if(y<-50||x<-50||x>880||y>660){
                x  = 100;
                y = 100;
            }
            //坦克随机新的方向
            dir();
        }
    }

    /**
     * 坦克随机方向
     */
    public void dir(){
        Dir[] values = Dir.values();
        //坦克移动到边界----转向---随机修改方向
        dir = values[new Random().nextInt(4)];
        //换坦克图片
//        System.out.println(dir);
        switch (dir){
            case right:
                this.bg = Toolkit.getDefaultToolkit().createImage(TankEntity.class.getResource("/tankR.png"));
                break;
            case left:
                this.bg = Toolkit.getDefaultToolkit().createImage(TankEntity.class.getResource("/tankL.png"));
                break;
            case up:
                this.bg = Toolkit.getDefaultToolkit().createImage(TankEntity.class.getResource("/tank.png"));
                break;
            case down:
                this.bg = Toolkit.getDefaultToolkit().createImage(TankEntity.class.getResource("/tankD.png"));
                break;
            default:
                System.out.println("------");
        }
    }


    /**
     * 判断坦克是否与矩形相撞
     * @param x
     * @param y
     * @param w
     * @param h
     * @return
     */
    public boolean isOnRect(int x,int y,int w,int h){
        //获取矩形
        Rectangle rectangle = new Rectangle(x, y, w, h);

        //判断该矩形框是否与坦克相交
        boolean contains = rectangle.intersects(this.x, this.y, this.w, this.h);

        return contains;
    }




    //构造方法


    public TankEntity() {
    }

    public TankEntity(int x, int y, int w, int h, Image bg, Dir dir) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
        this.bg = bg;
        this.dir = dir;
    }

    @Override
    public String toString() {
        return "TankEntity{" +
                "x=" + x +
                ", y=" + y +
                ", w=" + w +
                ", h=" + h +
                ", bg=" + bg +
                ", dir=" + dir +
                '}';
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getW() {
        return w;
    }

    public void setW(int w) {
        this.w = w;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    public Image getBg() {
        return bg;
    }

    public void setBg(Image bg) {
        this.bg = bg;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }
}
